<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/25
 * Time: 3:36 PM
 */

namespace api\models\rules;


use common\models\VipAssignment;
use yii\rbac\Item;
use yii\rbac\Rule;

class GoodsRule extends Rule
{
    //使用商品
    public $name = "useGood";

    /**
     * Executes the rule.
     *
     * @param string|int $user the user ID. This should be either an integer or a string representing
     * the unique identifier of a user. See [[\yii\web\User::id]].
     * @param Item $item the role or permission that this rule is associated with
     * @param array $params parameters passed to [[CheckAccessInterface::checkAccess()]].
     *
     * @return bool a value indicating whether the rule permits the auth item it is associated with.
     */
    public function execute($user, $item, $params)
    {
        $assignment = VipAssignment::findOne(['item_name' => $item->name, 'user_id' => $user]);
        if ($assignment->availability_num > 0 && $assignment->expired_time > time()) {
            return true;
        } else {
            return false;
        }
    }
}